package cn.gok.homework;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        School school1=new School("理工","泉州",1);
//        School school2=new School("职业技术","晋江",0);
        School[] school={new School("理工","泉州",1),new School("职业技术","晋江",0)};
        writeToFile(school);
        readFromFile();
        ArrayList<Student> students = new ArrayList<Student>();
        students.add(new Student(2, "李四", 21));
        students.add(new Student(3, "王五", 22));
        students.add(new Student(1, "张三", 20));
        students.add(new Student(3, "赵六", 23));
        sortFromSidAndAgeByComparatorAndCollections(students);
        System.out.println();
        sortFromSidAndAgeByComparable(students);

    }

    private static void writeToFile(School[] school) throws IOException {
        //文件输出流
        FileOutputStream fos=new FileOutputStream("1.dat");
        //对象输出流
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        oos.writeObject(school);
        System.out.println("写入完毕！！");
        oos.close();
    }
    private static void readFromFile() throws ClassNotFoundException, IOException {
        //文件输入流
        FileInputStream fis=new FileInputStream("1.dat");
        //对象输入流
        ObjectInputStream ois=new  ObjectInputStream(fis);
        School[] school= (School[]) ois.readObject();//相等于object obj=ois.readObject();
//        School school2=(School) ois.readObject();
        System.out.println(school[0]);
        System.out.println(school[1]);
//        System.out.println(school2);
        ois.close();
    }


    public static void sortFromSidAndAgeByComparatorAndCollections(ArrayList<Student> students){
        MyComparator myComparator = new MyComparator();
        Collections.sort(students,myComparator);
        for (Student i :students){
            System.out.println(i.toString());
        }
    }
    public static void sortFromSidAndAgeByComparable(ArrayList<Student> students){
        Collections.sort(students);
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
    }
}

